package com.topisv.tms.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class ExpressRule {

	/**
	 * 以1递增
	 * 
	 * @param firstExpress
	 * @return
	 */
	private static String getNextExpress(String firstExpress) {
		Long temp=Long.parseLong(firstExpress);
		if(temp==0){
			throw new IllegalArgumentException();
		}
		return temp + 1 + "";
	}
	
	/**
	 * 圆通
	 * @param firstExpress
	 * @return
	 */
	private static String getYTExpress(String firstExpress) {
		return getNextExpress(firstExpress);
	}


	/**
	 * 生成申通快递单的规则
	 * 
	 * @param nextExpress
	 * @return
	 */
	private static String getSTExpress(String firstExpress) {
		return getNextExpress(firstExpress);
	}
	
	/**
	 * 生成邮政小包快递单的规则
	 * @param firstExpress
	 * @return
	 */
	private static String getPOSTBExpress(String firstExpress){
		return getNextExpress(firstExpress);
	}
	/**
	 * EMS运单号规则
	 * 
	 * @param firstExpress
	 * @return
	 */
	private static String getEMSExpress(String firstExpress) {
		firstExpress = getNextExpress(firstExpress.substring(0,
				firstExpress.length() - 1));
		char[] numbers = firstExpress.toCharArray();
		int temp = getInteger(numbers[0]) * 8 + getInteger(numbers[1]) * 6
				+ getInteger(numbers[2]) * 4 + getInteger(numbers[3]) * 2
				+ getInteger(numbers[4]) * 3 + getInteger(numbers[5]) * 5
				+ getInteger(numbers[6]) * 9 + getInteger(numbers[7]) * 7;
		int intexTemp = temp % 11;
		intexTemp = 11 - intexTemp;
		if (intexTemp == 10) {
			return firstExpress + "0";
		} else if (intexTemp == 11) {
			return firstExpress + "5";
		} else {
			return firstExpress + intexTemp;
		}
	}

	private static int getInteger(char number) {
		return Integer.parseInt(number + "");
	}

	/**
	 * 顺风快递运单号推到规则
	 * 
	 * @param firstExpress
	 * @return
	 */
	private static String getSFExpress(String firstExpress) {
		if (firstExpress.length() == 12) {
			String nextExpress = getNextExpress(firstExpress.substring(0, 11));
			String twelve = firstExpress.substring(11, 12);
			String eleven = firstExpress.substring(10, 11);
			String ten = firstExpress.substring(9, 10);
			String nine = firstExpress.substring(8, 9);
			String eight = firstExpress.substring(7, 8);
			String seven = firstExpress.substring(6, 7);
			String six = firstExpress.substring(5, 6);
			String five = firstExpress.substring(4, 5);
			String four = firstExpress.substring(3, 4);
			if (!eleven.equals("9")) {
				int temp = Integer.parseInt(twelve) + 9;
				if (temp >= 10) {
					temp = temp - 10;
				}
				return nextExpress + temp;
			} else {
				if (ten.equals("3") || ten.equals("6")) {
					int temp = Integer.parseInt(twelve) + 5;
					if (temp >= 10) {
						temp = temp - 10;
					}
					return nextExpress + temp;
				} else if (ten.equals("9")) {
					if (nine.equals("9")) {
						if (eight.equals("9")) {
							if (seven.equals("9")) {
								if (six.equals("9")) {
									if (five.equals("9")) {
										if (Integer.parseInt(four) % 2 == 1) {
											return getSFExpress(twelve,
													nextExpress, 4);
										} else {
											return getSFExpress(twelve,
													nextExpress, 5);
										}
									} else if (five.equals("3")
											|| five.equals("6")) {
										return getSFExpress(twelve,
												nextExpress, 8);
									} else {
										return getSFExpress(twelve,
												nextExpress, 9);
									}
								} else if (six.equals("")) {
									return getSFExpress(twelve, nextExpress, 3);
								}
							} else if (seven.equals("0")) {
								return getSFExpress(twelve, nextExpress, 7);
							} else {
								return getSFExpress(twelve, nextExpress, 6);
							}
						} else if (eight.equals("0") || eight.equals("3")
								|| eight.equals("6")) {
							return getSFExpress(twelve, nextExpress, 0);
						} else {
							return getSFExpress(twelve, nextExpress, 9);
						}
					} else if (nine.equals("1") || nine.equals("3")
							|| nine.equals("5") || nine.equals("7")) {
						return getSFExpress(twelve, nextExpress, 2);
					} else {
						return getSFExpress(twelve, nextExpress, 3);
					}
				} else {
					return getSFExpress(twelve, nextExpress, 6);
				}
			}
		}
		return null;
	}

	private static String getSFExpress(String end, String nextExpress,
			int number) {
		int temp = Integer.parseInt(end) + number;
		if (temp >= 10) {
			temp = temp - 10;
		}
		return nextExpress + temp;
	}

	/**
	 * 根据相应快递单正则表达式生成相应快递单号
	 * 
	 * @param rex
	 * @param firstExpress
	 * @return
	 */
	public static String getExpress(String code, String firstExpress) {
		// 生成申通快递运单号
		if (code.equals("STO")) {
			return getSTExpress(firstExpress);
		} else if (code.equals("EMS")) {// EMS
			String first = firstExpress.substring(0, 2);
			String body = firstExpress.substring(2, firstExpress.length() - 2);
			String end = firstExpress.substring(firstExpress.length() - 2,
					firstExpress.length());
			return first + getEMSExpress(body) + end;
		} else if (code.equals("YTO")) {// 圆通速递
			return getYTExpress(firstExpress);
		} else if (code.equals("SF")) {// 顺风速运
			return getSFExpress(firstExpress);
		} else if("POSTB".equals(code)){//邮政小包
			return getPOSTBExpress(firstExpress);
		} else if("MJZT".equals(code)){//买家自提
			String first = firstExpress.substring(0, 2);
			return first + getNextExpress(firstExpress.substring(2));
		} else {
			return getNextExpress(firstExpress);
		}
	}

	/**
	 * 对输入的起始快递单号进行校验
	 * 
	 * @param firstExpressNumber
	 * @param rex
	 * @return
	 */
	public static boolean checkExpressNumber(String firstExpressNumber,
			String rex) {
		Pattern pattern = Pattern.compile(rex);
		Matcher matcher = pattern.matcher(firstExpressNumber);
		if (matcher.find()) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean checkNumber(String item) {
		String rex_one = "^[1-9][0-9]*$";
		Pattern patternOne = Pattern.compile(rex_one);
		Matcher matcherOne = patternOne.matcher(item);

		while (matcherOne.find()) {
			return true;
		}
		return false;
	}

	private static void test() {
		String rex_one = "^[0-9]+(.[0-9]{1,2})?$";
		String rex_two = "^[1-9][0-9]*$";

		String tempText = "012536532565";
		Pattern patternOne = Pattern.compile(rex_one);
		Matcher matcherOne = patternOne.matcher(tempText);

		Pattern patternTwo = Pattern.compile(rex_two);
		Matcher matcherTwo = patternTwo.matcher(tempText);
		while (matcherOne.find()) {
		}
		while (matcherTwo.find()) {
		}
	}

	public static void main(String args[]) {
	}
}
